package com.playball.lottery.fragment.frm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.playball.lottery.R;
import com.playball.lottery.ApplicationData;
import com.playball.lottery.customview.ShowTipDialog;
import com.playball.lottery.eventmanger.EventManager;
import com.playball.lottery.eventmanger.EventType;
import com.playball.lottery.eventmanger.EventManager.EventHandler;
import com.playball.lottery.frm.ads.AdvZx;
import com.playball.lottery.frm.kjinfo.KjinfoBDResult;
import com.playball.lottery.frm.kjinfo.KjinfoBasketBallResult;
import com.playball.lottery.frm.kjinfo.KjinfoListByType;
import com.playball.lottery.frm.kjinfo.KjinfoMatchResult;
import com.playball.lottery.nt.orjdd.GetRequestParams;
import com.playball.lottery.util.DateUtil;
import com.playball.lottery.util.LotConstants;
import com.playball.lottery.widget.PullToRefresh.PullToRefreshListView;


public class KjinfoMainFragment extends Fragment {
	public static final String TAG = "KjinfoMainFragment";

	View _RootView;

	PullToRefreshListView lv_main;
	private ListView mListView;
	TextView btn_title_left;
	View btn_right;
	TextView tv_title;
	ApplicationData app;
	// View frm_tips;
	// View kjinfo_lv_tip;
	// ProgressBar pb;
	// TextView tv_load_tips;
	KjInfoAdapter adapter = null;
	Button kjinfo_bt_quanbucai, kjinfo_bt_shuzicai, kjinfo_bt_kuaikaicai, kjinfo_bt_zucai;
	/** 0全部,1数字彩,2快开彩,3足彩 */
	int lotType = 0;
	// boolean isfirst=true;
	/** 最开始的获取的全部数据 */
	private List<Map<String, Object>> AllDataList;
	/** 页面显示的listview数据 */
	private List<Map<String, Object>> mDataList;

	private ShowTipDialog showdialog = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		app = ApplicationData.getInstance();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		_RootView = inflater.inflate(R.layout.kjinfo_mainfrm, null);
		return _RootView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		lv_main = (PullToRefreshListView) _RootView.findViewById(R.id.kjinfo_main_lv);
		mListView = lv_main.getRefreshableView();
		btn_right = _RootView.findViewById(R.id.top_title_right_btn);
		tv_title = (TextView) _RootView.findViewById(R.id.top_title_info);
		btn_title_left = (TextView) _RootView.findViewById(R.id.top_title_back);
		btn_title_left.setVisibility(View.INVISIBLE);
		// frm_tips = _RootView.findViewById(R.id.kjinfo_frm_tips);
		// kjinfo_lv_tip = _RootView.findViewById(R.id.kjinfo_lv_tip);
		// tv_load_tips = (TextView)
		// _RootView.findViewById(R.id.kjinfo_tv_load_tips);
		// pb = (ProgressBar) _RootView.findViewById(R.id.kjinfo_pb_load);
		kjinfo_bt_shuzicai = (Button) _RootView.findViewById(R.id.kjinfo_bt_shuzicai);
		kjinfo_bt_kuaikaicai = (Button) _RootView.findViewById(R.id.kjinfo_bt_kuaikaicai);
		kjinfo_bt_zucai = (Button) _RootView.findViewById(R.id.kjinfo_bt_zucai);
		kjinfo_bt_quanbucai = (Button) _RootView.findViewById(R.id.kjinfo_bt_quanbucai);
		kjinfo_bt_quanbucai.setSelected(true);
//		kjinfo_bt_quanbucai.setTextColor(_RootView.getResources().getColor(R.color.blue_ssqfont));

		EventManager.getInstance().registEventHandler(EventType.EV_BLL_UI_DAL_KJINFO_ALL_LASTED_GET,
				new EventHandler() {
					@Override
					public void excute(Object obj) {

						showdialog.closeDialog();
						// TODO Auto-generated method stub
						try {
							if (obj != null) {
								JSONTokener jsonParser = new JSONTokener((String) obj);
								JSONObject js = (JSONObject) jsonParser.nextValue();
								// 接下来的就是JSON对象的操作了
								if (Integer.parseInt(js.getString("Code")) == 0) {
									getData((String) obj);
									if (AllDataList != null) {
										AllDataList.clear();
									}
									if (mDataList != null) {
										mDataList.clear();
									}
									AllDataList = new ArrayList<Map<String, Object>>(app.KjData);
//									safelyRestoreUi();
									// Log.i("kjmainfrm",
									// "setupUiEvents EventManager.getInstance().registEventHandle");
									// mActivity.refreshView();
								}
							}
						} catch (NumberFormatException e) {
							e.printStackTrace();
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});

		lv_main.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {

			@Override
			public void onRefresh() {
				if(lv_main.getRefreshType() == 1) { //下拉
					requestKJInfo();
				}
			}
		});

		btn_title_left.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				EventManager.getInstance().onEvent(EventType.EV_UI_BLL_SCROLL_TO_LEFT, null);
			}
		});

		/*
		 * 2014.07.07 hudandan 添加资讯入口
		 */
		btn_right.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				getActivity().startActivity(new Intent(getActivity(), AdvZx.class));
			}
		});

		// 添加点击
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> list, View view, int position, long id) {
				int lotid = (Integer) mDataList.get(position - 1).get("LottID");
				String lottName = mDataList.get(position - 1).get("LottName").toString();
				Intent intent;
				if (lotid == LotConstants.LOT_ID_JCZQ) {// 90
					intent = new Intent(getActivity(), KjinfoMatchResult.class);
				} else if (lotid == LotConstants.LOT_ID_JCLQ) {// 91
					intent = new Intent(getActivity(), KjinfoBasketBallResult.class);
				} else if (lotid == LotConstants.LOT_ID_BD) {// 45
					intent = new Intent(getActivity(), KjinfoBDResult.class);
				} else {
					intent = new Intent(getActivity(), KjinfoListByType.class);
				}
				intent.putExtra("LottID", lotid + "");
				intent.putExtra("LottName", lottName);
				getActivity().startActivity(intent);
			}
		});

		setBtnOnClick(R.id.kjinfo_bt_quanbucai);
		setBtnOnClick(R.id.kjinfo_bt_shuzicai);
		setBtnOnClick(R.id.kjinfo_bt_kuaikaicai);
		setBtnOnClick(R.id.kjinfo_bt_zucai);

		tv_title.setText("开奖公告");

		requestKJInfo();
	}

	private void restoreUi() {
//		btn_right.setVisibility(View.VISIBLE);
//		((TextView) btn_right).setText("资讯");

		if (showdialog != null) {
			showdialog.closeDialog();
		}
		if (app.KjData == null || true == app.KjData.isEmpty()) {
			requestKJInfo();

		} else if (app.KjData.size() == 0) {
		} else {
			if (mDataList != null) {
				mDataList = new ArrayList<Map<String, Object>>();
				mDataList.clear();
				mDataList.addAll(getDataByLotType());
			}
			mDataList = new ArrayList<Map<String, Object>>();
			mDataList = getDataByLotType();
			adapter = new KjInfoAdapter(getActivity(), mDataList);
			mListView.setAdapter(adapter);
			lv_main.onRefreshComplete();
		}
	}

	public void onResume() {
		super.onResume();
		//统计页面
	}
	public void onPause() {
		super.onPause();
	}

	private List<Map<String, Object>> getData(String strResult) {
		try {
			JSONTokener jsonParser = new JSONTokener(strResult);
			JSONObject js = (JSONObject) jsonParser.nextValue();
			app.KjData = com.playball.lottery.util.ConvertJsonToList.getList(js.getString("Data"));
			for (int i = 0; i < app.KjData.size(); i++) {
				String winNumber = app.KjData.get(i).get("WinNumber").toString();

				String[] nums = winNumber.split("\\+");
				if ("1".equals(app.KjData.get(i).get("LottID").toString())) {// 胜负彩,(任选9)修改格式
					// 30033311300101转为3-0-0-3-3-3-1-1-3-0-0-1-0-1
					String s1 = "";
					if (!"".equals(winNumber)) {
						s1 = nums[0].substring(0, 1) + "-" + nums[0].substring(1, 2) + "-" + nums[0].substring(2, 3)
								+ "-" + nums[0].substring(3, 4) + "-" + nums[0].substring(4, 5) + "-"
								+ nums[0].substring(5, 6) + "-" + nums[0].substring(6, 7) + "-"
								+ nums[0].substring(7, 8) + "-" + nums[0].substring(8, 9) + "-"
								+ nums[0].substring(9, 10) + "-" + nums[0].substring(10, 11) + "-"
								+ nums[0].substring(11, 12) + "-" + nums[0].substring(12, 13) + "-"
								+ nums[0].substring(13, 14);
					}
					app.KjData.get(i).put("Ball", s1);
					String s2 = "";
					if (!"".equals(winNumber)) {
						s2 = winNumber.substring(0, 1) + "-" + winNumber.substring(1, 2) + "-"
								+ winNumber.substring(2, 3) + "-" + winNumber.substring(3, 4) + "-"
								+ winNumber.substring(4, 5) + "-" + winNumber.substring(5, 6) + "-"
								+ winNumber.substring(6, 7) + "-" + winNumber.substring(7, 8) + "-"
								+ winNumber.substring(8, 9) + "-" + winNumber.substring(9, 10) + "-"
								+ winNumber.substring(10, 11) + "-" + winNumber.substring(11, 12) + "-"
								+ winNumber.substring(12, 13) + "-" + winNumber.substring(13, 14);
					}
					app.KjData.get(i).put("WinNumber", s2);
				} else {
					app.KjData.get(i).put("Ball", nums[0].replaceAll(",", ""));
				}

				app.KjData.get(i).put("BlueBall", nums.length > 1 ? /* "+ " + */nums[1].trim() : "");

				String issue = app.KjData.get(i).get("Issue").toString();
				// issue += "期";
				// issue="第"+issue+"期     "+app.KjData.get(i).get("EndTime").toString();
				issue = "第" + issue + "期  " + DateUtil.getDateTime5(app.KjData.get(i).get("EndTime").toString());
				app.KjData.get(i).put("Issue", issue);
				app.KjData.get(i).put("Kjnumber", "开奖号码：");
				int lotID = Integer.parseInt(app.KjData.get(i).get("LottID").toString());
				switch (lotID) {
					case 3:// 七星彩
						app.KjData.get(i).put("img", R.drawable.buy_main_n7x);
						app.KjData.get(i).put("LottName", "七星彩");
						app.KjData.get(i).put("LotOrder", 13);
						break;
					case 5:// 双色球
						app.KjData.get(i).put("img", R.drawable.buy_main_ssq);
						app.KjData.get(i).put("LottName", "双色球");
						app.KjData.get(i).put("LotOrder", 1);
						break;
					case 6:// 福彩3D
						app.KjData.get(i).put("img", R.drawable.buy_main_3d);
						app.KjData.get(i).put("LottName", "福彩3D");
						app.KjData.get(i).put("LotOrder", 9);
						break;
					case 13:// 七乐彩
						app.KjData.get(i).put("img", R.drawable.buy_main_n7l);
						app.KjData.get(i).put("LottName", "七乐彩");
						app.KjData.get(i).put("LotOrder", 10);
						break;
					case 39:// 大乐透
						app.KjData.get(i).put("img", R.drawable.buy_main_dlt);
						app.KjData.get(i).put("LottName", "大乐透");
						app.KjData.get(i).put("LotOrder", 2);
						break;
					case 62:
						app.KjData.get(i).put("img", R.drawable.buy_main_11five);
						app.KjData.get(i).put("LottName", "十一运夺金");
						app.KjData.get(i).put("LotOrder", 4);
						break;
					case 63:// 排列3
						app.KjData.get(i).put("img", R.drawable.buy_main_p3);
						app.KjData.get(i).put("LottName", "排列3");
						app.KjData.get(i).put("LotOrder", 11);
						break;
					case 64:// 排列5
						app.KjData.get(i).put("img", R.drawable.buy_main_p5);
						app.KjData.get(i).put("LottName", "排列5");
						app.KjData.get(i).put("LotOrder", 12);
						break;
					case 70:// 11选5
						app.KjData.get(i).put("img", R.drawable.buy_main_11five);
						app.KjData.get(i).put("LottName", "11选5");
						app.KjData.get(i).put("LotOrder", 3);
						break;
//				case 28:// 重庆时时彩
//					app.KjData.get(i).put("img", R.drawable.buy_main_11five);
//					app.KjData.get(i).put("LottName", "重庆时时彩");
//					app.KjData.get(i).put("LotOrder", 14);
//					break;
					case 66:// LOT_ID_KUAI3
						app.KjData.get(i).put("img", R.drawable.buy_main_11five);// 图片待换
						app.KjData.get(i).put("LottName", "快三");
						app.KjData.get(i).put("LotOrder", 15);
						break;
					case 1:// 胜负彩/任选9
						app.KjData.get(i).put("img", R.drawable.buy_main_sfc);
						app.KjData.get(i).put("LottName", "胜负彩/任选9");
						app.KjData.get(i).put("LotOrder", 8);
						break;
//				case 45://北单
//					app.KjData.get(i).put("img", R.drawable.buy_main_jclq);
//					app.KjData.get(i).put("LottID", 45);
//					app.KjData.get(i).put("LottName", "北京单场");
//					app.KjData.get(i).put("Issue", "");// bdMap.put("Issue", "赛果查询");
//					app.KjData.get(i).put("LotOrder", 6);
//					break;
					case 90://zuqiu
						app.KjData.get(i).put("img", R.drawable.buy_main_jc);
						app.KjData.get(i).put("LottID", 90);
						app.KjData.get(i).put("LottName", "竞彩足球");
						app.KjData.get(i).put("Issue", "");// jcMap.put("Issue", "赛果查询")
						app.KjData.get(i).put("LotOrder", 5);
						break;
					case 91://lanqiu
						app.KjData.get(i).put("img", R.drawable.buy_main_jc);
						app.KjData.get(i).put("LottID", 91);
						app.KjData.get(i).put("LottName", "竞彩篮球");
						app.KjData.get(i).put("Issue", "");// jclqMap.put("Issue", "赛果查询");
						app.KjData.get(i).put("LotOrder", 7);
						break;
				}

			}
			// 添加查询足球竞彩赛果选项
//			Map<String, Object> jcMap = new HashMap<String, Object>();
//			jcMap.put("img", R.drawable.buy_main_jc);
//			jcMap.put("LottID", 90);
//			jcMap.put("LottName", "竞彩足球");
//			jcMap.put("Issue", "");// jcMap.put("Issue", "赛果查询")
//			jcMap.put("LotOrder", 5);
//			app.KjData.add(jcMap);
//			// 添加查询篮球竞彩赛果选项
//			Map<String, Object> jclqMap = new HashMap<String, Object>();
//			jclqMap.put("img", R.drawable.buy_main_jc);
//			jclqMap.put("LottID", 91);
//			jclqMap.put("LottName", "竞彩篮球");
//			jclqMap.put("Issue", "");// jclqMap.put("Issue", "赛果查询");
//			jclqMap.put("LotOrder", 7);
//			app.KjData.add(jclqMap);
//			// 添加查询北京单场赛果选项
//			Map<String, Object> bdMap = new HashMap<String, Object>();
//			bdMap.put("img", R.drawable.buy_main_jclq);
//			bdMap.put("LottID", 45);
//			bdMap.put("LottName", "北京单场");
//			bdMap.put("Issue", "");// bdMap.put("Issue", "赛果查询");
//			bdMap.put("LotOrder", 6);
//			app.KjData.add(bdMap);

			// 彩种排序 如果某彩种没有添加“LotOrder” 开奖信息不会显示此彩种
			List<Map<String, Object>> tempData = new ArrayList<Map<String, Object>>();
			int length = app.KjData.size();
			int curOrder = app.KjData.size();
			while (app.KjData.size() > 0) {
				int minInde = 0;
				int index = 0;
				for (Map<String, Object> map : app.KjData) {
					if ((Integer) map.get("LotOrder") != null && (Integer) map.get("LotOrder") < curOrder) {
						curOrder = (Integer) map.get("LotOrder");
						minInde = index;
					}
					index += 1;
				}
				tempData.add(app.KjData.get(minInde));
				app.KjData.remove(minInde);
				curOrder = length;
			}
			// 删除19(任选9).已便显示视图.任选9和胜负彩开奖信息一起显示
			for (int i = 0; i < tempData.size(); i++) {
				String LottID = tempData.get(i).get("LottID").toString();
				if ("19".equals(LottID)) {
					tempData.remove(i);
				}
			}
			app.KjData = tempData;
		} catch (Exception e) {
			// TODO: handle exception
			// e.printStackTrace();
		}
		// Log.v("kj", "getData app.KjData= "+app.KjData.toString());
		return app.KjData;
	}

	/**
	 * 根据按钮彩种类型筛选数据
	 *
	 * @param strResult
	 * @return
	 */
	private List<Map<String, Object>> getDataByLotType() {
		List<Map<String, Object>> tempData = new ArrayList<Map<String, Object>>();
		if (lotType == 0) {
			if (AllDataList != null) {
				tempData = new ArrayList<Map<String, Object>>(AllDataList);
			}
		} else if (lotType == 1) {// 高频彩
			if (AllDataList != null && AllDataList.size() != 0) {
				for (int i = 0; i < AllDataList.size(); i++) {
					// String LottID = tempData.get(i).get("LottID").toString();
					Map<String, Object> OldMap = AllDataList.get(i);
					int LottID = Integer.parseInt(AllDataList.get(i).get("LottID").toString());
					// Log.d("kj", "getDataByLotType 1 LottID= "+LottID);
					if (LottID == 5 || LottID == 6 || LottID == 39 || LottID == 63 || LottID == 64 || LottID == 3
							|| LottID == 13) {

						Map<String, Object> NewMap = new HashMap<String, Object>();
						NewMap.put("BlueBall", String.valueOf(OldMap.get("BlueBall")));
						NewMap.put("Ball", String.valueOf(OldMap.get("Ball")));
						NewMap.put("Issue", String.valueOf(OldMap.get("Issue")));
						NewMap.put("LotOrder", String.valueOf(OldMap.get("LotOrder")));
						NewMap.put("WinNumber", String.valueOf(OldMap.get("WinNumber")));
						NewMap.put("img", Long.parseLong(String.valueOf(OldMap.get("img"))));
						NewMap.put("EndTime", String.valueOf(OldMap.get("EndTime")));
						NewMap.put("LottName", String.valueOf(OldMap.get("LottName")));
						NewMap.put("Kjnumber", String.valueOf(OldMap.get("Kjnumber")));
						NewMap.put("LottID", LottID);
						tempData.add(NewMap);
					}
				}
			}
		} else if (lotType == 2) {
			if (AllDataList != null && AllDataList.size() != 0) {
				for (int i = 0; i < AllDataList.size(); i++) {
					// String LottID = tempData.get(i).get("LottID").toString();
					Map<String, Object> OldMap = AllDataList.get(i);
					int LottID = Integer.parseInt(AllDataList.get(i).get("LottID").toString());
					// Log.d("kj", "getDataByLotType 2 LottID= "+LottID);
					if (LottID == 70 || LottID == 62 || LottID == 28 || LottID == 66) {

						Map<String, Object> NewMap = new HashMap<String, Object>();
						NewMap.put("BlueBall", String.valueOf(OldMap.get("BlueBall")));
						NewMap.put("Ball", String.valueOf(OldMap.get("Ball")));
						NewMap.put("Issue", String.valueOf(OldMap.get("Issue")));
						NewMap.put("LotOrder", String.valueOf(OldMap.get("LotOrder")));
						NewMap.put("WinNumber", String.valueOf(OldMap.get("WinNumber")));
						NewMap.put("img", Long.parseLong(String.valueOf(OldMap.get("img"))));
						NewMap.put("EndTime", String.valueOf(OldMap.get("EndTime")));
						NewMap.put("LottName", String.valueOf(OldMap.get("LottName")));
						NewMap.put("Kjnumber", String.valueOf(OldMap.get("Kjnumber")));
						NewMap.put("LottID", LottID);
						tempData.add(NewMap);
					}
				}
			}
		} else if (lotType == 3) {
			if (AllDataList != null && AllDataList.size() != 0) {
				for (int i = 0; i < AllDataList.size(); i++) {
					// String LottID = tempData.get(i).get("LottID").toString();
					Map<String, Object> OldMap = AllDataList.get(i);
					int LottID = Integer.parseInt(AllDataList.get(i).get("LottID").toString());
					// Log.d("kj", "getDataByLotType 3 LottID= "+LottID);
					if (LottID == 1 || LottID == 19 /*|| LottID == 45*/ || LottID == 90 || LottID == 91) {
						Map<String, Object> NewMap = new HashMap<String, Object>();
						NewMap.put("BlueBall", String.valueOf(OldMap.get("BlueBall")));
						NewMap.put("Ball", String.valueOf(OldMap.get("Ball")));
						NewMap.put("Issue", String.valueOf(OldMap.get("Issue")));
						NewMap.put("LotOrder", String.valueOf(OldMap.get("LotOrder")));
						NewMap.put("WinNumber", String.valueOf(OldMap.get("WinNumber")));
						NewMap.put("img", Long.parseLong(String.valueOf(OldMap.get("img"))));
						NewMap.put("EndTime", String.valueOf(OldMap.get("EndTime")));
						NewMap.put("LottName", String.valueOf(OldMap.get("LottName")));
						NewMap.put("Kjnumber", String.valueOf(OldMap.get("Kjnumber")));
						NewMap.put("LottID", LottID);
						tempData.add(NewMap);
					}
				}
			}
		}
		// Log.d("kj",
		// "lotType= "+lotType+"  ,getDataByLotType tempData= "+tempData.toString());
		return tempData;
	}

	public void requestKJInfo() {
		AsyncLoader task = new AsyncLoader();
		String[] dyn = new String[2];
		JSONObject params = new JSONObject();
		dyn[0] = params.toString();
		dyn[1] = "300";
		task.execute(dyn);
//		((JddMainActivity)getActivity()).addTask(new MyTask("300", EventType.EV_BLL_UI_DAL_KJINFO_ALL_LASTED_GET, "{}"));
	}

	class AsyncLoader extends AsyncTask<String, Integer, String> {// 继承AsyncTask

		String action;
		ShowTipDialog showdialog = new ShowTipDialog();
		protected void onPreExecute() {
			showdialog.showDialog(getActivity());
		}
		@Override
		protected String doInBackground(String... paramsArr) {
			String strResult = null;
			action = paramsArr[1];
			strResult = GetRequestParams.getRequestResult(paramsArr[0], action, getActivity());
			return strResult;
		}

		protected void onProgressUpdate(Integer... progress) {
		}
		protected void onPostExecute(String result) {
//			hemai_lv_loading.setVisibility(View.GONE);
			showdialog.closeDialog();
			if (result != null) {
				if (action.equals("300")) {
					try {
						JSONTokener jsonParser = new JSONTokener(result);
						JSONObject js = (JSONObject) jsonParser.nextValue();
						// 接下来的就是JSON对象的操作了
						if (Integer.parseInt(js.getString("Code")) == 0) {
							getData(result);
							if (AllDataList != null) {
								AllDataList.clear();
							}
							if (mDataList != null) {
								mDataList.clear();
							}
							AllDataList = new ArrayList<Map<String, Object>>(app.KjData);
							restoreUi();
						}
					} catch (NumberFormatException e) {
						e.printStackTrace();
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			} else {
				Toast.makeText(getActivity(), getActivity().getString(R.string.problem_01), Toast.LENGTH_LONG).show();
			}

		}

		// 5.完成后操作
		protected void onCancelled() {
		}

	}

	private void setBtnOnClick(final int btnId) {
		Button btn = (Button) _RootView.findViewById(btnId);
		btn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				lotType = getLotType(btnId);
				// kjinfo_mainfrm_ll.setVisibility(View.GONE);
				Log.v("setBtnOnClick", "setBtnOnClick");
				Log.v("setBtnOnClick", "lotType= " + lotType);
				if (mDataList != null) {
					// mDataList = new ArrayList<Map<String, Object>>();
					mDataList.clear();
					mDataList.addAll(getDataByLotType());
					Log.v("setBtnOnClick", "mDataList= " + mDataList.toString());
				}
				if (adapter != null) {
					Log.v("setBtnOnClick", "notifyDataSetChanged 执行");
					adapter.notifyDataSetChanged();
				} else {
					mDataList = new ArrayList<Map<String, Object>>();
					mDataList = getDataByLotType();
					adapter = new KjInfoAdapter(getActivity(), mDataList);
					mListView.setAdapter(adapter);
				}

			}
		});
	}

	/**
	 * 获取lotType，及改变按钮背景
	 *
	 * @param btnId
	 * @return
	 */
	public int getLotType(int btnId) {
		// 恢复默认
		kjinfo_bt_shuzicai.setSelected(false);
		kjinfo_bt_kuaikaicai.setSelected(false);
		kjinfo_bt_zucai.setSelected(false);
		kjinfo_bt_quanbucai.setSelected(false);
		// bt_all.setTextColor(context.getResources().getColor(R.color.black));
//		kjinfo_bt_quanbucai.setTextColor(_RootView.getResources().getColor(R.color.black));
//		kjinfo_bt_shuzicai.setTextColor(_RootView.getResources().getColor(R.color.black));
//		kjinfo_bt_kuaikaicai.setTextColor(_RootView.getResources().getColor(R.color.black));
//		kjinfo_bt_zucai.setTextColor(_RootView.getResources().getColor(R.color.black));
		switch (btnId) {
			case R.id.kjinfo_bt_quanbucai:
				lotType = 0;
				kjinfo_bt_quanbucai.setSelected(true);
//			kjinfo_bt_quanbucai.setTextColor(_RootView.getResources().getColor(R.color.blue_ssqfont));
				break;
			case R.id.kjinfo_bt_shuzicai:
				lotType = 1;
				kjinfo_bt_shuzicai.setSelected(true);
//			kjinfo_bt_shuzicai.setTextColor(_RootView.getResources().getColor(R.color.blue_ssqfont));
				break;
			case R.id.kjinfo_bt_kuaikaicai:
				lotType = 2;
				kjinfo_bt_kuaikaicai.setSelected(true);
//			kjinfo_bt_kuaikaicai.setTextColor(_RootView.getResources().getColor(R.color.blue_ssqfont));
				break;
			case R.id.kjinfo_bt_zucai:
				lotType = 3;
				kjinfo_bt_zucai.setSelected(true);
//			kjinfo_bt_zucai.setTextColor(_RootView.getResources().getColor(R.color.blue_ssqfont));
				break;
		}
		return lotType;
	}

	class KjInfoAdapter extends BaseAdapter {

		private LayoutInflater inflater = null;

		private List<Map<String, Object>> mData;

		public KjInfoAdapter(Context context, List<Map<String, Object>> mData) {
			inflater = LayoutInflater.from(context);
			this.mData = mData;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mData.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			ViewHolder holder = null;
			// if(convertView == null){
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.kjinfo_item, null);
			holder.name = (TextView) convertView.findViewById(R.id.kjinfo_item_name);
			holder.issue = (TextView) convertView.findViewById(R.id.kjinfo_item_issue);
			holder.ballLayout = (LinearLayout) convertView.findViewById(R.id.kjinfo_item_ball_layout);
			convertView.setTag(holder);
			// }else{
			// holder = (ViewHolder) convertView.getTag();
			// }
			holder.name.setText(mData.get(position).get("LottName").toString());
			holder.issue.setText(mData.get(position).get("Issue").toString());
			final int lotid = (Integer) mData.get(position).get("LottID");
			LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			layoutParams.gravity = Gravity.CENTER_VERTICAL;
			layoutParams.rightMargin = 5;
			if (lotid == 90) {// 竞彩足球
				View view = inflater.inflate(R.layout.kjinfo_item_jczq, null);
				holder.ballLayout.setPadding(0, 0, 0, 0);
				holder.ballLayout.addView(view, layoutParams);
			} else if (lotid == 91) {
				View view = inflater.inflate(R.layout.kjinfo_item_jclq, null);
				holder.ballLayout.setPadding(0, 0, 0, 0);
				holder.ballLayout.addView(view, layoutParams);
			} else if (lotid == 45) {
				View view = inflater.inflate(R.layout.kjinfo_item_bd, null);
				holder.ballLayout.setPadding(0, 0, 0, 0);
				holder.ballLayout.addView(view, layoutParams);
			} else if (lotid == 1) {
				ImageView imageView = new ImageView(getActivity());
				imageView.setImageResource(R.drawable.kjinfo_item_zqsfc);
				TextView view = (TextView) inflater.inflate(R.layout.kjinfo_item_sfc, null);
				view.setText(mData.get(position).get("Ball").toString());
				holder.ballLayout.setPadding(0, 0, 0, 0);
				holder.ballLayout.addView(imageView);
				holder.ballLayout.addView(view, layoutParams);
			} else if(lotid == 66){//快3
				String redBTemp = mData.get(position).get("Ball").toString();
				char[] cs;
				if (!"".equals(redBTemp)) {
					cs = redBTemp.toCharArray();
					if (cs[1] != ' ') {
						redBTemp = "";
						for (int i = 0; i < cs.length; i++) {
							if (i == 0) {
								redBTemp = redBTemp + cs[0];
							} else {
								redBTemp = redBTemp + " " + cs[i];
							}
						}
					}
				}
				if (!"".equals(redBTemp)) {
					String[] redBall = redBTemp.split(" ");
					for (String ball : redBall) {
						TextView button = (TextView) inflater.inflate(R.layout.kjinfo_item_ball, null);
//						button.setText(ball);
						if (ball.equals("1")) {
							button.setBackgroundResource(R.drawable.kuai3_1);
						}else if(ball.equals("2")){
							button.setBackgroundResource(R.drawable.kuai3_2);
						}else if(ball.equals("3")){
							button.setBackgroundResource(R.drawable.kuai3_3);
						}else if(ball.equals("4")){
							button.setBackgroundResource(R.drawable.kuai3_4);
						}else if(ball.equals("5")){
							button.setBackgroundResource(R.drawable.kuai3_5);
						}else{
							button.setBackgroundResource(R.drawable.kuai3_6);
						}
						LayoutParams kuai3_params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
						kuai3_params.gravity = Gravity.CENTER_VERTICAL;
						kuai3_params.rightMargin = 10;
						holder.ballLayout.addView(button, kuai3_params);
					}
				}
			}else {
				String redBTemp = mData.get(position).get("Ball").toString();
				String blueBTemp = mData.get(position).get("BlueBall").toString();
				if (lotid == 3 || lotid == 6 || lotid == 63 || lotid == 64/* || lotid == 28*/) {
					char[] cs;
					if (!"".equals(redBTemp)) {
						cs = redBTemp.toCharArray();
						if (cs[1] != ' ') {
							redBTemp = "";
							for (int i = 0; i < cs.length; i++) {
								if (i == 0) {
									redBTemp = redBTemp + cs[0];
								} else {
									redBTemp = redBTemp + " " + cs[i];
								}
							}
						}
					}
					cs = blueBTemp.toCharArray();
					blueBTemp = "";
					for (int i = 0; i < cs.length; i++) {
						if (i == 0) {
							blueBTemp = blueBTemp + cs[0];
						} else {
							blueBTemp = blueBTemp + " " + cs[i];
						}
					}
				}
				if (!"".equals(redBTemp)) {
					String[] redBall = redBTemp.split(" ");
					for (String ball : redBall) {
						TextView button = (TextView) inflater.inflate(R.layout.kjinfo_item_ball, null);
						button.setText(ball);
//						button.setTextColor(getResources().getColor(R.color.ui_new_btn_yes_normal_solid));
//						button.setBackgroundResource(R.drawable.redball_normal);
						button.setTextColor(getResources().getColor(R.color.white));
						button.setBackgroundResource(R.drawable.redball_selected);
						holder.ballLayout.addView(button, layoutParams);
					}
				}

				String[] blueBall = blueBTemp.split(" ");
				for (String ball : blueBall) {
					if (ball != "") {
						TextView button = (TextView) inflater.inflate(R.layout.kjinfo_item_ball, null);
						button.setText(ball);
//						button.setTextColor(getResources().getColor(R.color.ui_new_btn_no_normal_solid));
//						button.setBackgroundResource(R.drawable.blueball_normal);
						button.setTextColor(getResources().getColor(R.color.white));
						button.setBackgroundResource(R.drawable.blueball_selected);
						holder.ballLayout.addView(button, layoutParams);
					}
				}
			}
			return convertView;
		}

		class ViewHolder {
			TextView name;
			TextView issue;
			LinearLayout ballLayout;
		}
	}
}
